package LifeParserCommon
{
	import CustomProviders.FBProvider;
	import CustomProviders.GMailProvider;
	
	import LifeParserCommon.ServiceInterface;
	
	import flashx.textLayout.formats.Float;
	
	import mx.collections.ArrayCollection;
	import mx.formatters.NumberBaseRoundType;
	import mx.formatters.NumberFormatter;
	
	[Bindable]
	public class ServiceProvider implements ServiceInterface
	{
		private var serviceObj: ServiceInterface= null;
		private var serviceType: String			= null;
		
		public function ServiceProvider(dataObj: Object, serviceType: String)
		{
			super();
			this.serviceType					= serviceType;
			
			if (serviceType == "GMAIL")
			{
				this.serviceObj					= new GMailProvider(dataObj ["data"]);
			}
			else if(serviceType == "FB")
			{
				this.serviceObj					= new FBProvider(dataObj ["data"]);
			}
		}
		
		public function getHourDataProvider(): ArrayCollection
		{
			var hourArray: Array				= new Array();
			var hourData: ArrayCollection		= new ArrayCollection();
			var idx: int						= 0;
			var providerData: ArrayCollection	= this.serviceObj.getHourDataProvider();
			
			//Initialize hourArray.
			
			for (idx = 1; idx <= 24; idx++)
			{
				hourArray[ idx ]				= 0;
			}
			
			for (idx = 0; idx < providerData.length; idx++)
			{
				var hour: int					= this.doFormatTimeToHour(providerData[ idx ]["time"]);
				hourArray[ hour + 1 ]++;
			}
			
			for (idx = 1; idx <= 24; idx++)
			{
				var dataObj: Object				= new Object();
				dataObj[ "hour" ]				= idx;
				dataObj[ "activityCount" ]		= hourArray[ idx ];
				
				hourData.addItem(dataObj);
			}
			
			return hourData;
		}
		
		public function getDayDataProvider(): ArrayCollection
		{
			return this.serviceObj.getDayDataProvider();
		}
		
		public function getMonthDataProvider(): ArrayCollection
		{
			var monthArray: Array				= new Array();
			var monthData: ArrayCollection		= new ArrayCollection();
			var idx: int						= 0;
			var providerData: ArrayCollection	= this.serviceObj.getMonthDataProvider();
			
			for (idx = 1; idx <= 12; idx++)
			{
				monthArray[ idx ]				= 0;
			}
			
			for (idx = 0; idx < providerData.length; idx++)
			{
				var month: int					= this.getMonthFromDate(providerData[ idx ][ "date" ]);
				monthArray[ month ]++;
			}
			
			for (idx = 1; idx <= 12; idx++)
			{
				var dataObj: Object				= new Object();
				dataObj[ "month" ]				= idx;
				dataObj[ "activityCount" ]		= monthArray[ idx ];
				
				monthData.addItem(dataObj);
			}
			
			return monthData;
		}
		
		public function getActivityDataProvider(): ArrayCollection
		{
			var providerData: ArrayCollection	= this.serviceObj.getActivityDataProvider();
			var activityData: Object			= new Object();
			var idx: int						= 0;
			var requestLabel: String			= "labelDay";
			var frequencyLabel: String			= providerData[ 0 ][ requestLabel + "frequencyParameter" ];
			
			for (idx = 0; idx < providerData.length; idx++)
			{
				var paramArray: Array			= providerData[ idx ][ frequencyLabel ].split(",");
				var paramIdx: int				= 0;
				
				for (paramIdx = 0; paramIdx < paramArray.length; paramIdx++)
				{
					if(activityData[ paramArray[ paramIdx ] ] == undefined)
					{
						var activityItem: Object				= new Object();

						activityItem[ "activityCount" ]			= 1;
						
						for (var key: String in providerData[ idx ])
						{
							activityItem[ key ]					= providerData[ idx ][ key ];
						}
						
						activityData[ paramArray[ paramIdx ] ]	= activityItem;
					}
					else
					{
						activityData[ paramArray[ paramIdx ] ][ "activityCount" ]	+= 1;
					}
				}
			}
			
			var retCollection: ArrayCollection		= new ArrayCollection();
			
			retCollection.addItem(activityData);
			
			return retCollection;
		}
		
		public function getSentimentDataProvider(): ArrayCollection
		{
			var providerData: ArrayCollection		= this.serviceObj.getSentimentDataProvider();
			var idx: int							= 0;
			var formatter: NumberFormatter			= new NumberFormatter();
			
			// Set Formatter Properties.
			formatter.rounding						= NumberBaseRoundType.NEAREST;
			formatter.precision						= 1;
			
			for (idx = 0; idx < providerData.length; idx++)
			{
				if (providerData [idx][ "Sentiment Subjectivity" ] == undefined)
				{
					var posValue: int				= parseInt(providerData [idx]["positiveSentiment"]);
					var negValue: int				= parseInt(providerData [idx]["negativeSentiment"]);
					var polarityValue: Number		= 0;
					var subjectivityValue: Number	= 0;
					
					if (posValue != 0 && negValue != 0)
					{
						subjectivityValue			= (Number)((posValue + negValue)/(posValue + negValue));
						polarityValue				= (Number)((posValue - negValue)/(posValue + negValue + 3));
					}
					
					providerData [idx]["Sentiment Subjectivity"]	= formatter.format(subjectivityValue);
					providerData [idx]["Sentiment Polarity"]		= formatter.format(polarityValue);
				}
			}
			
			return providerData;
		}
		
		public function doFormatTimeToHour(time: String): Number
		{
			return this.serviceObj.doFormatTimeToHour(time);
		}
		
		public function getMonthFromDate(date: String): int
		{
			return this.serviceObj.getMonthFromDate(date);
		}
		
		public function getServiceType(): String
		{
			return this.serviceType;
		}
	}
}